﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BeanboxSoftware.BeanProxy
{
  static class IEnumerableExtension
  {
    public static T Find<T>(this IEnumerable<T> e, Predicate<T> predicate)
    {
      foreach (var it in e.FindAll(predicate))
      {
        return it;
      }

      throw new ArgumentNullException("No match was found");
    }
    public static T Find<T>(this IEnumerable<T> e, Predicate<T> predicate, T def)
    {
      foreach (var it in e.FindAll(predicate))
      {
        return it;
      }

      return def;
    }
    public static IEnumerable<T> FindAll<T>(this IEnumerable<T> e, Predicate<T> predicate)
    {
      foreach (T t in e)
      {
        if (predicate(t))
        {
          yield return t;
        }
      }
    }
  }
}
